package com.mgr.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mgr.model.Accounts;
import com.mgr.service.AccountService;
import com.mgr.service.CustomerService;
import com.mgr.util.EncryptionNumeric;

@Controller
public class ViewInfoController {
	
	@Autowired
	CustomerService custService;
	
	@Autowired
	AccountService accService;
	
	@RequestMapping("view_balance")
	public String getViewBalance(HttpSession session, Map<String, Object> map, HttpServletRequest request) {
		String result = "viewbalance.tile";
		String id = (String) session.getAttribute("user");
		EncryptionNumeric coder = new EncryptionNumeric();
		if(id == null || id == "") result = "login";
		else {
			List<Accounts> accountList = accService.getAccountByCustId(Integer.parseInt(id));
			String[][] accCodeList = new String[2][accountList.size()];
			int i = 0;
			for(Accounts acc : accountList) {
				accCodeList[0][i] = String.valueOf(acc.getAccId());
				accCodeList[1][i] = coder.encoding(String.valueOf(acc.getAccId()));
				i++;
			}
			map.put("accountList", accountList);
			map.put("accIdList", accCodeList);
			String accId = request.getParameter("acc");
			if(accId != null && !accId.isEmpty()) {
				map.put("accCode", accId);
				accId = coder.decoding(accId);				
				map.put("account", accService.getAccount(Integer.parseInt(accId)));	
			}			
		}
		return  result;
	}
}